#coding=utf-8
#LightCam SSH检查程序
#init by hfh 20150313

import time
import ssh
import lc_libs
import traceback,sys

while True:


	lchome=lc_libs.lc_getlchome()
	scname=sys.argv[0].split('.')[0]
	logfilename=lchome+'/log/'+scname+'.log'
	confname=lchome+'/conf/'+scname+'.conf'
	logfile=file(logfilename,'a')
	logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logfile.write(logdate+" Started.\n")
	logfile.flush()


	try:
		c=ssh.SSHClient()
		c.set_missing_host_key_policy(ssh.AutoAddPolicy())
		c.connect("172.23.240.41",port=22,username="monitor",password="!cffEx2007\\")
		stdin,stdout,stderr=c.exec_command("show chassis fpc 0")
		for line in stdout.read().split('\n'):
			if 'Online' in line:
				while '  ' in line:
					line=line.replace('  ',' ')
				line=line.strip().split()
				strs='Slot:'+line[0]+' State:'+line[1]+' Temp(C):'+line[2]+' CPU:Total'+line[3]+'%,Interrupt:'+line[4]+'% Memory:DRAM:'+line[5]+' Heap:'+line[6]+' Buffer:'+line[7]
				if int(line[6]) > 90:
					Manager='ywn_monitor1'
					Node='172.23.240.41'
					AlertGroup='NetCheckHeap'
					AlertKey='ZJ_JR_JR_F51'
					Severity='4'
					Type='1'
					Summary=strs
					
					strs=lc_libs.lc_gen_alert(Manager,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					print strs
					lc_libs.lc_alert(strs)
		
		c.close()

	except Exception:
		excinfo=traceback.format_exc()
		logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
		logfile.write(logdate+" "+excinfo+"\n")
		logfile.flush()
		time.sleep(1)

	time.sleep(86400)
